// import Map3DControl from '@/utils/gis/Map3DControl.js'

import GTileLayer from '@/utils/gis/GTileLayer.js'

export default function setupCesiumMap() {
  const map = new CgsMap({
    target: 'mapContainer',
    projection: 'EPSG:102113',
    center: [118.804, 32.864],
    zoom: 11,
    mode: '3D',
    threeFlag: true,
  })
  window.map = map
  // window.map3DControl = new Map3DControl(map)

  map.loadConfig()
  map.setStarVisible(true)
  map.setFogVisible(false)

  // map.hideToolBar()
  // map.showNavigation()

  document.querySelector('.toolbar_container').style.display = 'none'

  loadTileLayer() //加载影像

  loadTerrain() //加载地形

  // 指北针
  // const navigation = new CesiumNavigation(map.map3D, {})

  // const viewer = window.map3DControl.viewer
  // viewer.screenSpaceEventHandler.setInputAction((movement) => {
  //   const earthPosition = window.map3DControl.viewer.scene.pickPosition(movement.position)
  //   const cartographic = Cesium.Cartographic.fromCartesian(earthPosition)
  //   const wgs = [
  //     (cartographic.longitude * 180) / Math.PI,
  //     (cartographic.latitude * 180) / Math.PI,
  //     cartographic.height,
  //   ]
  //   console.log('笛卡尔坐标:' + earthPosition)
  //   console.log('wgs84坐标:' + wgs)
  // }, Cesium.ScreenSpaceEventType.RIGHT_CLICK)
}

/** 加载影像 */
function loadTileLayer() {
  //默认全球影像
  const defaultLayerOptions = {
    url: 'http://49.77.124.50:32010/earthview/services/Globe/RasterService/WMTS',
    layer: 'Globe',
    text: '全球影像',
    level: 11,
    visible: true,
    isAddToLayerTree: true,
    tileMatrixLabels: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
    pId: 'level1_1',
    type: 'wmts',
    proxy: false,
  }
  new GTileLayer().add({ name: 'qqyx', parentName: 'qqyx', option: defaultLayerOptions })

  const layerOptions = {
    url: 'https://services.arcgisonline.com/arcgis/rest/services/World_Imagery/MapServer/WMTS',
    layer: 'World_Imagery',
    text: '在线世界影像地图',
    level: 19,
    visible: true,
    isAddToLayerTree: true,
    tileMatrixLabels: [
      0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
    ],
    pId: 'level1_1',
    type: 'wmts',
    proxy: false,
  }

  new GTileLayer().add({ name: 'qqyx', parentName: 'qqyx', option: layerOptions })
}

/** 加载地形 */
function loadTerrain() {
  //设置cesiumToken
  Cesium.Ion.defaultAccessToken =
    'eyJqdGkiOiJiNDEzM2I4OS1hMDU2LTRjYzEtYjVhMy0zMWJjZmQyMTk4N2MiLCJpZCI6NTkxOTcsImlhdCI6MTYzMDAzMzM2OH0'

  //加载地形
  // window.map3DControl.viewer.terrainProvider = Cesium.createWorldTerrain({
  //   requestWaterMask: false, // required for water effects
  //   requestVertexNormals: true, // required for terrain lighting
  // })
}
